﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Toolbox_0._0._1;
using System.Windows.Threading;
using Microsoft.Devices;

namespace Appkit
{
    public partial class Radomizer : PhoneApplicationPage
    {
        private int fromValue = 0;
        private int toValue = 2;

        private Random generator;
        private DispatcherTimer timer;

        private int counter = 1;

        private VibrateController vc;
        
        public Radomizer()
        {
            InitializeComponent();
            vc = VibrateController.Default;
            this.fromSelector.DataSource = new IntLoopingDataSource() { MinValue = int.MinValue, MaxValue = int.MaxValue, SelectedItem = 0, PrevValue = 0 };
            this.toSelector.DataSource = new IntLoopingDataSource() { MinValue = int.MinValue, MaxValue = int.MaxValue, SelectedItem = 2, PrevValue = 2 };

        }

        //Event fire when start random number
        private void Radom_Event(object sender, RoutedEventArgs e)
        {
            vc.Start(TimeSpan.FromMilliseconds(100));
            fromValue = (int)fromSelector.DataSource.SelectedItem;
            toValue = (int)toSelector.DataSource.SelectedItem;

            if (fromValue > toValue)
            {
                int tmp=fromValue;
                fromValue = toValue;
                toValue = tmp;
            }


            btnStart.IsEnabled = false;

            //while (bwardCounter != 10)
            //{
            //    generator = new Random((int)DateTime.Now.Ticks);
            //    counter = bwardCounter * bwardCounter * 10;
            //    Thread.Sleep(counter);
            //    bwardCounter++;
            //    lbRndNumber.Text = generator.Next(fromValue, toValue + 1) + "";
            //}
            //bwardCounter = 0;
            if (timer == null)
            {
                timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromMilliseconds(counter*counter/3);
                timer.Tick += OnTick;
            }
            timer.Start();
        }

        //An event fire after a certain amount of time
        private void OnTick(Object sender, EventArgs args)
        {
            counter++;
            generator = new Random((int)DateTime.Now.Ticks);
            lbRndNumber.Text = generator.Next(fromValue, toValue + 1) + "";
            timer.Interval = TimeSpan.FromMilliseconds(counter * counter/3);

            if (counter == 30)
            {
                timer.Stop();
                btnStart.IsEnabled = true;
                counter = 1;
            }
        }

       
        private void Help_Click(object sender, RoutedEventArgs e)
        {
            vc.Start(TimeSpan.FromMilliseconds(100));
            MessageBox.Show("-Choose the range of numbers to randomize.\n-Long press the slider to enter by keyboard.\n-Press the button or shake to random a number.", "Help", MessageBoxButton.OK);
        }

        private void From_LongPressed(object sender, System.Windows.Input.GestureEventArgs e)
        {
            
        }

    }
}